package com.heima.behavior.service.impl;

import com.heima.behavior.service.UnLikesBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadUtil.AppThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UnLikesBehaviorServiceImpl implements UnLikesBehaviorService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 不喜欢文章行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikesBehavior(UnLikesBehaviorDto dto) {
        //1.校验参数
        if (dto.getArticleId() == null) {
            return ResponseResult.okResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.获取登录用户id
        ApUser apUser = AppThreadLocalUtil.getUser();
        //表示未登录
        if(apUser==null){
            return ResponseResult.okResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //游客身份不能进行以下操作
        if(apUser.getId()==0){
            return ResponseResult.okResult(405,"游客身份禁止，需进行登录");
        }
        //5.redis查询是否有用户不喜欢文章
        String unlike_key = BehaviorConstants.USER_UNLIKES_ + dto.getArticleId() + "";
        Boolean likeKeyFlag = stringRedisTemplate.opsForHash().hasKey(unlike_key, apUser.getId() + "");
        //5.1是，并且正在执行的是不喜欢操作，直接返回
        if (likeKeyFlag && dto.getType() == 0) {
            return ResponseResult.okResult("不喜欢该文章");
        }
        //5.1.1是，并且执行的是取消不喜欢，删除redis，mongodb
        if (likeKeyFlag && dto.getType() == 1) {
            //删除redis
            stringRedisTemplate.opsForHash().delete(unlike_key, apUser.getId() + "");
            //删除mongodb
            Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("userId").is(apUser.getId()));
            mongoTemplate.remove(query, ApUnlikesBehavior.class);

            return ResponseResult.okResult("还是喜欢的");
        }
        //5.2否，正在执行不喜欢操作 新增redis，mongodb
        if (!likeKeyFlag && dto.getType() == 0) {
            //新增redis
            stringRedisTemplate.opsForHash().put(unlike_key, apUser.getId() + "", "1");
            //新增mongodb
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setId(ObjectId.get().toHexString());
            apUnlikesBehavior.setUserId(Long.valueOf(apUser.getId()));
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setType((short)0);
            apUnlikesBehavior.setCreatedTime(new Date());

            mongoTemplate.save(apUnlikesBehavior);
        }
        return ResponseResult.okResult("不喜欢该文章");

    }


}
